package com.cdks.wuxiu.api.controller;

import com.cdks.wuxiu.api.model.common.AuthParam;
import com.cdks.wuxiu.api.model.dto.WalletInfoDTO;
import com.cdks.wuxiu.api.model.param.WithdrawAddAccountParam;
import com.cdks.wuxiu.api.model.param.WithdrawDelAccountParam;
import com.cdks.wuxiu.api.model.param.WithdrawHomeParam;
import com.cdks.wuxiu.api.model.param.WithdrawCreateOrderParam;
import com.cdks.wuxiu.api.model.param.WithdrawGetRecordParam;
import com.cdks.wuxiu.api.service.UserService;
import com.cdks.wuxiu.api.service.WalletService;
import com.cdks.wuxiu.api.service.WithdrawService;
import com.cdks.wuxiu.api.util.TimeUtils;
import com.cdks.wuxiu.common.model.api.ApiResult;
import com.cdks.wuxiu.entity.mysql.UserBaseInfo;
import com.cdks.wuxiu.entity.mysql.WithdrawAccountInfo;
import com.cdks.wuxiu.entity.mysql.WithdrawMoneyConfig;
import com.cdks.wuxiu.entity.mysql.WithdrawOrderInfo;
import com.cdks.wuxiu.entity.mysql.WithdrawRatioConfig;

import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Api(tags = "提现")
@RestController
@RequestMapping("/withdraw")
public class WithDrawController {
    @Resource
    private WithdrawService withdrawService;
    @Resource
    private WalletService walletService;
    @Resource
    private UserService userService;


    @ApiOperation("提现首页")
    @PostMapping("/home")
    public ApiResult<Object> home(@RequestBody @Valid WithdrawHomeParam param) {
        //先更新接通率
        UserBaseInfo userInfo  = param.getUserInfo();
        userService.updateListenerInfo(userInfo);
        //根据提现账号获取提现比例配置
        WithdrawRatioConfig ratioCfg  = withdrawService.getWithdrawRatio(userInfo, param.getAcctId());
        if (ratioCfg == null)
        {
            return ApiResult.failed(10, "提现账号有误。详情请咨询客服");
        }

        //获取账户余额
        WalletInfoDTO wallet  = walletService.getUserWalletWithScore(userInfo.getUserId(), TimeUtils.getTimeSecond());
        int ratio  = (TimeUtils.getTimeSecond() - userInfo.getCreateTime()) < 2592000 ? 80 : ratioCfg.getRatio();
        long balance  = ((wallet.getCoin() + wallet.getScore()) * ratio / 10000);

        List<String> hint  = new ArrayList<>();
        hint.add("1、24小时内只能申请一次提现，次日到账。节假日顺延");
        hint.add("2、有待处理提现订单时不能申请新的提现");
        hint.add("3、提现金额以实际到账为准");

        JSONObject vo  = new JSONObject();
        vo.set("balance", balance);
        vo.set("moneyList", withdrawService.getWithdrawMoneyListByType(ratioCfg.getType()));
        vo.set("hint", hint);

        return ApiResult.success(vo);
    }

    @ApiOperation("添加提现账号")
    @PostMapping("/addAccount")
    public ApiResult<Object> addAccount(@RequestBody @Valid WithdrawAddAccountParam param) {
        WithdrawAccountInfo account  = new WithdrawAccountInfo();
        account.setCreateTime(TimeUtils.getTimeSecond());
        account.setInfo(param.getInfo());
        account.setUserId(param.getUserId());
        account.setType(param.getType());
        
        int ret  = withdrawService.addAccount(account);
        if (ret < 0)
        {
            return ApiResult.failed(10, "添加账号失败");
        }

        return ApiResult.success();
    }

    @ApiOperation("删除提现账号")
    @PostMapping("/delAccount")
    public ApiResult<Object> delAccount(@RequestBody @Valid WithdrawDelAccountParam param) {       
        int ret  = withdrawService.delAccount(param.getUserId(), param.getAcctId());
        if (ret < 0)
        {
            return ApiResult.failed(10, "删除账号失败");
        }

        return ApiResult.success();
    }

    @ApiOperation("获取提现账号列表")
    @PostMapping("/getAccountList")
    public ApiResult<Object> getAccountList(@RequestBody @Valid AuthParam param) {
        List<WithdrawRatioConfig> configs  = withdrawService.getWithdrawType();
        if (configs.isEmpty())
        {
            return null;
        }

        List<Integer> types = new ArrayList<>();
        for (WithdrawRatioConfig config : configs)
        {
            types.add(config.getType());
        }

        JSONObject json  = new JSONObject();
        json.set("validType", types);
        json.set("acctList", withdrawService.getAccountList(param.getUserId(), types));

        return ApiResult.success(json);
    }

    @ApiOperation("创建提现订单")
    @PostMapping("/createOrder")
    public ApiResult<Object> createOrder(@RequestBody @Valid WithdrawCreateOrderParam param) {
        //根据提现账号获取提现比例配置
        WithdrawRatioConfig ratioCfg  = withdrawService.getWithdrawRatio(param.getUserInfo(), param.getAcctId());
        if (ratioCfg == null)
        {
            return ApiResult.failed(10, "提现失败，提现账号有误。详情请咨询客服");
        }

        //检查用户是否可以提现
        withdrawService.checkUserStatus(param.getUserId());

        int withDrawMoney  = param.getMoney();
        WithdrawMoneyConfig feeCfg  = withdrawService.getWithdrawFeeByMoney(ratioCfg.getType(), withDrawMoney);
        if (feeCfg == null)
        {
            return ApiResult.failed(10, "提现失败，提现金额有误。");
        }

        int percent  = (TimeUtils.getTimeSecond() - param.getUserInfo().getCreateTime()) < 2592000 ? 80 : ratioCfg.getRatio();
        WithdrawOrderInfo order  = new WithdrawOrderInfo();
        order.setId(0L);
        order.setUserId(param.getUserId());
        order.setAcctId(param.getAcctId());
        order.setRatio(percent);
        order.setFee(withDrawMoney * feeCfg.getFee() / 100);
        order.setType(ratioCfg.getType());        
        order.setMoney(withDrawMoney);

        int ret = withdrawService.createOrder(order, (withDrawMoney * 1000 / percent) * 10);
        if (ret < 0)
        {
            return ApiResult.failed(10, "提现失败");
        }

        return ApiResult.success();
    }

    @ApiOperation("获取提现明细")
    @PostMapping("/getRecord")
    public ApiResult<Object> getRecord(@RequestBody @Valid WithdrawGetRecordParam param) {
        List<WithdrawOrderInfo> recordList  = withdrawService.getRecord(param.getUserId(), param.getPage(), param.getStart(), param.getStop());
        
        JSONObject json  = new JSONObject();
        json.set("recordList", recordList);

        return ApiResult.success(json);
    }
}
